package com.yanqu.road.server.manger;

import com.yanqu.road.logic.bussiness.servercenter.ChannelBussiness;
import com.yanqu.road.entity.servercenter.ChannelVersion;
import com.yanqu.road.server.TempMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelVersionMgr extends TempMgr {

	private static final Logger logger = LogManager.getLogger(ChannelVersionMgr.class.getName());

	private static List<ChannelVersion> channelVersionList;

	private static Map<Integer, Map<Integer, ChannelVersion>> lastReleaseChannelVersionMap;

	private static Map<Integer, Map<Integer, ChannelVersion>> lastWhiteReleaseChannelVersionMap;

	@Override
	public boolean stop() {
		return false;
	}

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean reloadData() {
		channelVersionList = ChannelBussiness.getChannelVersionList();
		initLastReleaseChannelVersionMap();
		return true;
	}

	@Override
	public boolean reloadConfig() throws Exception {
		return true;
	}

	private void initLastReleaseChannelVersionMap(){
		Map<Integer, Map<Integer, ChannelVersion>> dataMap = new ConcurrentHashMap<>();
		Map<Integer, Map<Integer, ChannelVersion>> whiteDataMap = new ConcurrentHashMap<>();
		for(ChannelVersion channelVersion : channelVersionList){
			if(channelVersion.isLastRelease()){
				if(!dataMap.containsKey(channelVersion.getChannelId())){
					dataMap.put(channelVersion.getChannelId(), new ConcurrentHashMap<>());
				}
				dataMap.get(channelVersion.getChannelId()).put(channelVersion.getPhonePlatform(), channelVersion);
			}
			if(channelVersion.isWhiteLastRelease()){
				if(!whiteDataMap.containsKey(channelVersion.getChannelId())){
					whiteDataMap.put(channelVersion.getChannelId(), new ConcurrentHashMap<>());
				}
				whiteDataMap.get(channelVersion.getChannelId()).put(channelVersion.getPhonePlatform(), channelVersion);
			}
		}
		lastReleaseChannelVersionMap = dataMap;
		lastWhiteReleaseChannelVersionMap = whiteDataMap;
	}

	//白名单先获取白名单版本，如果没有设置，通用普通版本
	public static ChannelVersion getLastReleaseVersion(int channelId, int phonePlatform, String ip, boolean isAdmin,String platformUserId){
		Map<Integer, ChannelVersion> dataMap;
		ChannelVersion channelVersion = null;
		if(WhiteIpMgr.isPlayerWhiteIp(ip) || PlatformUserIdMgr.isPlatformUserIdPass(platformUserId) || isAdmin){
			dataMap = lastWhiteReleaseChannelVersionMap.get(channelId);
			if(null != dataMap){
				channelVersion = dataMap.get(phonePlatform);
			}
		}
		if(null == channelVersion){
			dataMap = lastReleaseChannelVersionMap.get(channelId);
			if(null != dataMap){
				channelVersion = dataMap.get(phonePlatform);
			}
		}
		return channelVersion;
	}

	public static ChannelVersion getChannelVersion(int channelId, int phonePlatform, String packageVersion){
		for(ChannelVersion channelVersion : channelVersionList){
			if(channelVersion.getChannelId() == channelId && channelVersion.getPhonePlatform() == phonePlatform &&
					Objects.equals(channelVersion.getClientVersion(), packageVersion)){
				return channelVersion;
			}
		}
		return null;
	}
}
